<template>
  <div class="container">
    <div class="topbar">
      <div class="title">处理列表</div>
      <span class="setting" @click="showMenu"><i class="el-icon-more"></i></span>
    </div>
    <el-menu
      @select="handleSelect"
      mode="horizontal"
      default-active="1"
      background-color="#409EFF"
      text-color="#fff"
      active-text-color="#fff">
      <el-menu-item index="0">全部</el-menu-item>
      <el-menu-item index="1">进行中</el-menu-item>
      <el-menu-item index="2">已处理</el-menu-item>
    </el-menu>
    <div class="content" ref="scrollcontainer" v-infinite-scroll="load" infinite-scroll-disabled="disabled">
      <el-card v-for="(item, index) in caseList" :key="item.id" shadow="alawys" class="letter-wrapper" :body-style="{padding: '0'}">
        <div class="letter-item" @click="showAction(item)">
          <div class="header">
            <span class="title">序号：{{index+1}}</span>
            <el-button type="text" style="color: #606266">{{item.caseIsAnonymity==='1' ? '匿名' : item.caseCreatorName}}</el-button>
          </div>
          <div class="detail">
            <!-- <div v-html="item.caseContent" style="padding: 10px 0;"></div> -->
            <div style="padding-top: 10px;">主题：{{item.caseTitle}}</div>
            <p class="item-status"><span>{{item.caseCreatetime}}</span><span :style="{color: item.caseStatus === '1' ? '#E6A23C' : '#409EFF'}">{{item.caseStatus === '1' ? '进行中' : '已完成'}}</span></p>
          </div>
        </div>
      </el-card>
      <div style="text-align: center; margin: 5px; color: #909399;" v-if="loading">加载中…</div>
    </div>
  </div>
</template>

<script>
import dingMixin from  '@/mixins/ding'
import formUtil from "@/utils/formUtil";
export default {
  data () {
    return {
      isFirstLoading: true,
      loading: false,
      page: 1,
      limit: 20,
      totalPage: 1,
      totalCount: 0,
      searchPost: {
        caseStatus: '1'
      },
      caseList: [],
      authority: {},
      currentScrollTop: 0,
    };
  },
  mixins: [dingMixin],
  computed: {
    noMore () {
      return this.page >= this.totalPage
    },
    disabled () {
      return this.isFirstLoading || this.loading || this.noMore
    },
    hasLimit() {
      return this.authority.userLevel === '1'
    }
  },
  beforeRouteLeave(to,from,next) {
    this.currentScrollTop = this.$refs.scrollcontainer.scrollTop;
    this.mobileStopRefresh()
    next()
  },
  activated() {
    if(window.NEEDREFRESH) {
      this.init();
      window.NEEDREFRESH = false;
    }else {
      this.$refs.scrollcontainer.scrollTop = this.currentScrollTop;
    }
  },
  async mounted () {
    if(this.$route.params.id) this.goLetter(this.$route.params.id, 'deal')
    this.init()
  },
  methods: {
    async init() {
      this.getCaseList(true);
      this.authority = await this.$httpN.postB('petition/authority/asd')
    },
    load () {
      this.loading = true;
      this.page ++;
      this.getCaseList();
    },
    handleSelect(key) {
      this.searchPost.caseStatus = (key==='0' ? '' : key);
      this.getCaseList(true);
    },
    goSetting() {
      this.$router.push({ name: 'letter-setting'})
    },
    goLetter(id, handleType) {
      this.$router.push({ name: 'letter-add', params: { id: id, handleType: handleType }})
    },
    async getCaseList(init) {
      if(init) {
        this.page = 1;
        this.caseList = [];
        this.isFirstLoading = true;
      }
      let {list, totalPage, totalCount} = await this.$httpN.postB('petition/case/handleList', {
        page: this.page,
        limit: this.limit,
        caseStatus: this.searchPost.caseStatus
      });
      this.loading = false;
      this.caseList.push(...list);
      if(init){
        this.totalPage = totalPage;
        this.totalCount = totalCount
        this.$nextTick(() => {
          this.isFirstLoading = false;
        })
      }
    },
    showAction(letter) {
      this.mobileSelect('更多操作', ['查看', '处理'],({buttonIndex}) => {
        if(buttonIndex === 0) {
          this.goLetter(letter.id, 'view')
        }else if(buttonIndex === 1) {
          if(letter.caseStatus!=='1') return this.mobileAlert('当前记录已处理完成');
          this.goLetter(letter.id, 'deal')
        }
      })
    },
    showMenu() {
      this.mobileSelect('更多操作', this.hasLimit ? ['我的上访列表', '设置'] : ['我的上访列表'],({buttonIndex}) => {
        if(buttonIndex === 0) {
          this.$router.push({ name: 'letter-mobileList'})
        }else if(buttonIndex === 1) {
          this.goSetting();
        }
      })
    }
  }
}
</script>

<style lang='scss' scoped>
.container {
  width: 100%;
  height: 100vh;
  .topbar {
    height: 50px;
    position: relative;
    box-shadow: 0 1px 4px rgba(0,0,0,0.08);
    .title {
      line-height: 50px;
      font-size: 18px;
      text-align: center;
    }
    .setting {
      position: absolute;
      right: 20px;
      top: 0;
      font-size: 18px;
      margin-top: 16px;
    }
  }
  .el-menu {
    display: flex;
    justify-content: space-between;
    .el-menu-item {
      width: 100%;
      text-align: center;
      height: 40px;
      line-height: 40px;
    }
    /deep/ .is-active{
      background-color: rgb(51, 126, 204) !important;
    }
  }
  .content {
    height: calc(100% - 90px);
    padding-bottom: 50px;
    overflow-y: auto;
    .letter-wrapper {
      margin: 20px 20px 0 20px;
      box-shadow: 0 2px 12px 0 rgba(0,0,0,.1);
      .letter-item {
        .header {
          padding: 0 10px;
          border-bottom: 1px solid #ebeef5;
          display: flex;
          justify-content: space-between;
          height: 36px;
          line-height: 36px;
          .title {
            overflow: hidden;
            text-overflow:ellipsis;
            white-space: nowrap;
          }
        }
        .detail {
          padding: 0 10px;
          overflow-x: auto;
          .item-status {
            display: flex;
            justify-content: space-between;
          }
        }
      }
    }
  }
}
</style>